import type { AxiosPromise } from "axios";
import { ElMessageBox, ElMessage } from "element-plus";

export default function (
  selectionList,
  ids,
  page,
  query,
  onLoadReq,
  delReq: (ids: any) => AxiosPromise,
  updateStatusReq: (ids: any, status: any) => AxiosPromise,
  showAddForm?,
  enableParams?, // 修改字典对应启用的值
  disableParams? // 修改字典对应禁用的值
) {
  const handleAdd = () => {
    if (showAddForm) showAddForm.value = true;
  };
  const handleDel = () => {
    if (selectionList.value.length) {
      ElMessageBox.confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delReq(ids.value).then((res: any) => {
            if (res.success) {
              ElMessage({
                type: "success",
                message: "操作成功!",
              });
            } else {
              ElMessage({
                type: "warning",
                message: res.msg,
              });
            }
            page.currentPage = 1;
            onLoadReq(page, query.value);
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        });
    } else {
      ElMessage({
        message: "请选择至少一条数据",
        type: "warning",
      });
    }
  };
  const handleEnable = () => {
    if (selectionList.value.length) {
      updateStatusReq(ids.value, enableParams || 0).then((res) => {
        ElMessage({
          type: "success",
          message: `成功启用`,
        });
        page.currentPage = 1;
        onLoadReq(page, query.value);
      });
      // ElMessage({
      //   message: 'success',
      //   type: 'success'
      // });
    } else {
      ElMessage({
        message: "请选择至少一条数据",
        type: "warning",
      });
    }
  };
  const handleDisable = () => {
    if (selectionList.value.length) {
      updateStatusReq(ids.value, disableParams || 1).then((res) => {
        ElMessage({
          type: "success",
          message: `成功禁用`,
        });
        page.currentPage = 1;
        onLoadReq(page, query.value);
      });
    } else {
      ElMessage({
        message: "请选择至少一条数据",
        type: "warning",
      });
    }
  };
  return {
    handleAdd,
    handleDel,
    handleEnable,
    handleDisable,
  };
}
